<?php

namespace admin\models;

use Yii;

/**
 * This is the model class for table "mm_user_family".
 *
 * @property integer $mf_id
 * @property integer $mu_id
 * @property integer $mr_id
 * @property string $mf_name
 * @property integer $mf_age
 * @property string $mf_type
 * @property string $mf_phone
 *
 * @property Info $mu
 */
class UserFamily extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mm_user_family';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mr_id','mf_name','mf_age','mf_type','mf_phone'],'required'],
            [['mu_id', 'mr_id', 'mf_age'], 'integer'],
//            [['mf_age'],'max'=>3],
            [['mf_name', 'mf_type'], 'string', 'max' => 20],
            [['mf_phone'], 'string', 'max' => 13],
            [['mf_phone'],'validPhone'],
            [['mu_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserInfo::className(), 'targetAttribute' => ['mu_id' => 'mi_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'mf_id' => '家庭成员表id',
            'mu_id' => '用户id',
            'mr_id' => '关系',
            'mf_name' => '姓名',
            'mf_age' => '年纪',
            'mf_type' => '工作类型',
            'mf_phone' => '联系方式',
        ];
    }
    
    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMu()
    {
        return $this->hasOne(UserInfo::className(), ['mi_id' => 'mu_id']);
    }

    public function getRealtion(){
        return $this->hasOne(Relation::className(),['mr_id'=>'mr_id']);
    }

    public function validPhone(){
        if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9][0-9]{8}|17[0-9]{9}$|14[0-9]{9}$/",$this->mf_phone) && strlen($this->mf_phone)==11){
            $this->addError('mf_phone','手机格式错误');
        }
    }
}
